 //
//  SHD_HomeCollectionViewCell.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_HomeCollectionViewCell.h"
#import "GZ_PlayerPageVC.h"
#import "GZ_RadioModel.h"
#import "GZ_RadioDetailsVC.h"
#import "XMN_ReadDetailViewController.h"
@implementation SHD_HomeCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getHomeData];
        
    }
    return self;
}
-(void)getHomeData
{
    _dataArray = [NSMutableArray array];
    _dataScourseArray = [NSMutableArray array];
    _SHD_contestTableView = [[UITableView alloc]init];
    _SHD_contestTableView.delegate = self;
    _SHD_contestTableView.dataSource = self;
    [_SHD_contestTableView NightWithType:UIViewColorType2];
    _SHD_contestTableView.tableHeaderView = [[myView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 200 * FitHeight)];
#pragma mark - 加载
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _SHD_contestTableView.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
    [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _SHD_contestTableView.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_SHD_contestTableView.header beginRefreshing];

    

    [self.contentView addSubview:_SHD_contestTableView];
    _backTopImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_backTopImageView];
    [self getData];
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.contentView addSubview:_HUD];
    



}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
        
        switch ([[_dataArray[indexPath.row] Type] integerValue]) {
            case 1:
            case 14:
            case 29:
            case 24:
            case 10:
            case 27:
            case 18:
            case 9:
                return 385 * FitHeight;
                break;
            case 5:return 190 * FitHeight;
                break;
                
            case 4:
            case 17:
                return 510 * FitHeight;
                break;
            case 2:
                return 340 * FitHeight;
                break;
            default:
                return 385 * FitHeight;
                break;
        }
    

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _i = indexPath.row;
    static NSString *identifier = @"HomeCell";
    SHD_HomePageTableView *homeCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!homeCell) {
        homeCell = [[SHD_HomePageTableView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    // _SHD_contestTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    homeCell.listModel = _dataArray[indexPath.row];
    homeCell.selectionStyle = UITableViewCellSelectionStyleNone;
    static NSString *musicIden = @"MusicCell";
    SHD_MusicTableViewCell *musicCell = [tableView dequeueReusableCellWithIdentifier:musicIden];
    if (!musicCell) {
        musicCell = [[SHD_MusicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:musicIden];
    }
    // _SHD_contestTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //musicCell.listModel = _dataArray[indexPath.row];
    musicCell.selectionStyle = UITableViewCellSelectionStyleNone;
    static NSString *photoIden = @"photoCell";
    SHD_PhotoTebleViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:photoIden];
    if (!photoCell) {
        photoCell = [[SHD_PhotoTebleViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:photoIden];
    }
    // _SHD_contestTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //musicCell.listModel = _dataArray[indexPath.row];
    photoCell.selectionStyle = UITableViewCellSelectionStyleNone;
    static NSString *talkIden = @"talkCell";
    SHD_TalkTableViewCell *talkCell = [tableView dequeueReusableCellWithIdentifier:talkIden];
    if (!talkCell) {
        talkCell = [[SHD_TalkTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:talkIden];
    }
    // _SHD_contestTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //musicCell.listModel = _dataArray[indexPath.row];
    talkCell.selectionStyle = UITableViewCellSelectionStyleNone;
    static NSString *tingIden = @"tingCell";
    SHD_TingTableViewCell *tingCell = [tableView dequeueReusableCellWithIdentifier:tingIden];
    if (!tingCell) {
        tingCell = [[SHD_TingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tingIden];
    }
    // _SHD_contestTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //musicCell.listModel = _dataArray[indexPath.row];
    tingCell.selectionStyle = UITableViewCellSelectionStyleNone;
#pragma mark cell 的动画效果
    homeCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        homeCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    
    musicCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        musicCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    
    talkCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        talkCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
   
    photoCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        photoCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    tingCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        tingCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    


    switch ([[_dataArray[indexPath.row] Type] integerValue]) {
        case 1:
        case 14:
        case 29:
        case 24:
        case 10:
        case 27:
        case 18:
        case 9:
            
            return homeCell;
            break;
        case 5:musicCell.musicList = _dataArray[indexPath.row];
            
            return musicCell;
            break;
        case 3:talkCell.talkModel = _dataArray[indexPath.row];
            return talkCell;
            break;
        case 17:
        case 4:photoCell.photoList = _dataArray[indexPath.row];
            return photoCell;
            break;
        case 2:tingCell.tingList = _dataArray[indexPath.row];
            return tingCell;
            break;
        default:
            return homeCell;
            break;
    }
    
    
}
#pragma mark - 解析数据
-(void)getData
{
    if ([_SHD_contestTableView.header isRefreshing]) {
        _flag = 0;
    }
    else
    {
        _flag += 10;
    }
    [HTTPMethod getDataByString:@"http://api2.pianke.me/pub/today" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&limit=10&start=%ld&version=3.0.6", _flag]] WithDataBlock:^(id data) {
        NSArray *dataArray = [[data objectForKey:@"data"] objectForKey:@"list"];
        _scrollArray = [NSMutableArray array];
        _scrollArray = [[data objectForKey:@"data"] objectForKey:@"carousel"];
        if (data != nil) {
            if (_flag == 0) {
                
                [_dataArray removeAllObjects];
            }
            
        }
        for (NSDictionary *dic in dataArray) {
            if ([[dic objectForKey:@"type"] integerValue] == 4 || [[dic objectForKey:@"type"] integerValue] == 17) {
                SHD_PhotoList *list = [[SHD_PhotoList alloc]init];
                [list setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:list];
            }
            else if ([[dic objectForKey:@"type"] integerValue] == 2)
            {
                SHD_TingList *list = [[SHD_TingList alloc]init];
                [list setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:list];
                for (NSDictionary *dataDic in list.playList) {
                    GZ_RadioModel *gzList = [[GZ_RadioModel alloc]init];
                    [gzList setValuesForKeysWithDictionary:dataDic];
                    [_dataScourseArray addObject:gzList];
                }
                
            }
            else{
                SHD_HomeList *list = [[SHD_HomeList alloc]init];
                [list setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:list];
            }
        }
        
        [_SHD_contestTableView reloadData];
        [_SHD_contestTableView.header endRefreshing];
        [_SHD_contestTableView.footer endRefreshing];
        [_HUD removeFromSuperview];
#pragma mark 滚动视图
        _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 200 * FitHeight)];
        [_SHD_contestTableView.tableHeaderView addSubview:_myScrollView];
        _myScrollView.contentSize = CGSizeMake(375 * FitWidth * (_scrollArray.count + 2), 200 * FitHeight);
        _myScrollView.contentOffset = CGPointMake(375 * FitWidth, 0);
        _myScrollView.pagingEnabled = YES;
        _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
        [_myImageView sd_setImageWithURL:[[_scrollArray lastObject] objectForKey:@"img"] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            NSLog(@"%ld", receivedSize / expectedSize);
        } completed:nil];
        [_myScrollView addSubview:_myImageView];
        for (NSInteger i = 0; i < _scrollArray.count; i++) {
            _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((i + 1)* 375 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
            _myImageView.tag = 1001 + i;
            _myImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            [_myImageView addGestureRecognizer:tap];
            [ _myImageView sd_setImageWithURL:[_scrollArray[i]  objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]] ;
            [_myScrollView addSubview:_myImageView];
        }
        _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((_scrollArray.count + 1)* 375 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
        [ _myImageView sd_setImageWithURL:[[_scrollArray firstObject]  objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]] ;
        _myScrollView.delegate = self;
        [_myScrollView addSubview:_myImageView];
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(260 * FitWidth, 180 * FitHeight, 90 * FitWidth, 20)];
        _page.numberOfPages = _scrollArray.count ;
        _page.currentPageIndicatorTintColor = [UIColor grayColor];
        _page.pageIndicatorTintColor = [UIColor whiteColor];
        _page.currentPage = 0;
        
        [_SHD_contestTableView.tableHeaderView addSubview:_page];
        if (_timer == nil) {
            
            _timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollView) userInfo:nil repeats:YES];
        }
        }];

}
-(void)getScroller
{
    _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 200 * FitHeight)];
    [_SHD_contestTableView.tableHeaderView addSubview:_myScrollView];
    _myScrollView.contentSize = CGSizeMake(375 * FitWidth * (_scrollArray.count + 2), 200 * FitHeight);
    _myScrollView.contentOffset = CGPointMake(375 * FitWidth, 0);
    _myScrollView.pagingEnabled = YES;
    _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
    [_myImageView sd_setImageWithURL:[[_scrollArray lastObject] objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]];
    
    [_myScrollView addSubview:_myImageView];
    for (NSInteger i = 0; i < _scrollArray.count; i++) {
        _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((i + 1)* 375 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
        _myImageView.tag = 1001 + i;
        _myImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_myImageView addGestureRecognizer:tap];
        [ _myImageView sd_setImageWithURL:[_scrollArray[i]  objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]] ;
        [_myScrollView addSubview:_myImageView];
    }
    _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((_scrollArray.count + 1)* 375 * FitWidth, 0, 375 * FitWidth, 200 * FitHeight)];
    [ _myImageView sd_setImageWithURL:[[_scrollArray firstObject]  objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]] ;
    _myScrollView.delegate = self;
    [_myScrollView addSubview:_myImageView];
    _myScrollView.showsHorizontalScrollIndicator = NO;
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(260 * FitWidth, 180 * FitHeight, 90 * FitWidth, 20)];
    page.numberOfPages = _scrollArray.count ;

    page.currentPageIndicatorTintColor = [UIColor grayColor];
    page.pageIndicatorTintColor = [UIColor whiteColor];
    page.currentPage = 0;
    
    [_SHD_contestTableView.tableHeaderView addSubview:page];
    _timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollView) userInfo:nil repeats:YES];

}
#pragma mark 滚动视图点击进入详情
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    NSArray *array = [[_scrollArray[imageView.tag - 1001]  objectForKey:@"url"] componentsSeparatedByString:@"//"];
    NSArray *arr = [array[1] componentsSeparatedByString:@"/"];
    if ([arr[0] isEqualToString:@"article"] || [arr[0] isEqualToString:@"topic"]) {
        [self.delegata passByValue:arr[1]];
    }
    else if ([arr[0] isEqualToString:@"timeline"])
    {
        SHD_DataTageViewController *sdv = [[SHD_DataTageViewController alloc]init];
        [self.nav pushViewController:sdv animated:YES];
        sdv.tag = arr[1];

    }
    else if ([arr[0] isEqualToString:@"fm"])
    {
        GZ_RadioDetailsVC *az = [[GZ_RadioDetailsVC alloc]init];
        [self.nav pushViewController:az animated:YES];
        az.radioString = arr[1];
    }
   
    
}
#pragma mark 实现循环滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_myScrollView == scrollView) {
        
        if (_myScrollView.contentOffset.x > 375 * FitWidth * (_scrollArray.count)) {
            _myScrollView.contentOffset = CGPointMake(0, 0);
        }
        else if (_myScrollView.contentOffset.x < 0)
        {
            _myScrollView.contentOffset = CGPointMake(375 * FitWidth * (_scrollArray.count), 0);
        }
        
        
        
    }
#pragma mark - 回顶部
    else
    {
       
        if (_i > 10) {
            [UIView animateWithDuration:1 animations:^{
                _backTopImageView.frame = CGRectMake(300 * FitWidth, 520 *FitHeight, 32 * FitWidth, 32 * FitHeight);
            }];
        }
        else
        {
            [UIView animateWithDuration:1 animations:^{
                _backTopImageView.frame = CGRectMake(300 * FitWidth, 700 *FitHeight, 32 * FitWidth, 32 * FitHeight);
            }];
        }
    }
}
#pragma mark 定时滚动
-(void)scrollView
{
    [_myScrollView setContentOffset:CGPointMake(375 * FitWidth + _myScrollView.contentOffset.x, 0) animated:YES];

    _page.currentPage =_myScrollView.contentOffset.x / (375 * FitWidth ) ;
    if (_myScrollView.contentOffset.x / (375 * FitWidth ) > _scrollArray.count  - 1) {
        _page.currentPage = 0;
        [_myScrollView setContentOffset:CGPointMake(0,0)];
    }else
    {
        _page.currentPage = _myScrollView.contentOffset.x / 375;
    }



}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_myScrollView == scrollView) {
       
        
        _page.currentPage = scrollView.contentOffset.x / (375 * FitWidth ) - 1;
        if (scrollView.contentOffset.x / (375 * FitWidth ) > _scrollArray.count) {
            _page.currentPage = 0;
        }
        else if (scrollView.contentOffset.x / (375 * FitWidth ) < 1)
        _page.currentPage = _scrollArray.count - 1;
        _timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollView) userInfo:nil repeats:YES];
        

    }
   
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

    
        [_timer invalidate];


    

    
}
#pragma mark 点击进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([[_dataArray[indexPath.row] Type] integerValue] == 2) {
        GZ_PlayerPageVC *gv = [[GZ_PlayerPageVC alloc]init];
        gv.tingString = [_dataArray[indexPath.row] ID];
        gv.dataSourceArray = _dataScourseArray;
        [self.nav pushViewController:gv animated:YES];
    }
    else
    {
        
        XMN_ReadDetailViewController *xmn = [[XMN_ReadDetailViewController alloc]init];
        [self.nav pushViewController:xmn animated:YES];
        xmn.idString = [_dataArray[indexPath.row] ID];
        xmn.titleString = [_dataArray[indexPath.row] title];
        xmn.imageString = [_dataArray[indexPath.row] coverimg];
        xmn.contentString = [_dataArray[indexPath.row] content];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _SHD_contestTableView.frame = CGRectMake(0, 0, 375 * FitWidth, (667 - 64 ) * FitHeight);
   _backTopImageView.frame = CGRectMake(300 * FitWidth, 700 *FitHeight, 32 * FitWidth, 32 * FitHeight);
    _backTopImageView.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    _backTopImageView.image = [UIImage imageNamed:@"SHD_回顶部"];
    _backTopImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backTop)];
    [_backTopImageView addGestureRecognizer:tap];
}
#pragma mark 回顶部触发方法
-(void)backTop
{
    [_SHD_contestTableView.header beginRefreshing];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
